package cn.ym.ymcc.service.impl;

import cn.ym.ymcc.domain.MessageSms;
import cn.ym.ymcc.mapper.MessageSmsMapper;
import cn.ym.ymcc.query.MessageSmsQuery;
import cn.ym.ymcc.result.PageList;
import cn.ym.ymcc.service.IMessageSmsService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 210513347@qq.com
 * @since 2023-05-05
 */
@Service
public class MessageSmsServiceImpl extends ServiceImpl<MessageSmsMapper, MessageSms> implements IMessageSmsService {

    @Autowired
    private MessageSmsMapper messageSmsMapper;
    @Override
    public void save(String title, String content, String phone) {
        MessageSms sms = new MessageSms();
        sms.setTitle(title);
        sms.setContent(content);
        sms.setPhone(phone);
        sms.setSendTime(new Date());
        //获取ip
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String ip = requestAttributes.getRequest().getRemoteAddr();
        sms.setIp(ip);
        //userid   如果是用户登录直接获取
        //sms.setUserId(1L);
        super.insert(sms);
    }

    @Override
    public PageList<MessageSms> queryPage(MessageSmsQuery query) {
        Wrapper<MessageSms> wrapper = new EntityWrapper<>();
        if(!StringUtils.isEmpty(query.getKeyword())){
            wrapper.like("title", query.getKeyword());
        }
        Long count = Long.valueOf(messageSmsMapper.selectCount(wrapper));
        if(count==0){
            return new PageList<>();
        }
        List<MessageSms> MessageSms = messageSmsMapper.queryData(query);
        return new PageList<>(count, MessageSms);
    }
}
